
local unit_attribute = require 'types.unit_attribute'
local register = unit_attribute.register
local data = load_excel('基础设定.xlsx','属性定义')

local list1 = {}
local list2 = {}
ac.attr_key1 = {}
ac.attr_key2 = {}
ac.attr_key3 = {}
ac.attribute = {}
--新自定义
for name,da in pairs(data) do
    local key = da.key or name
    da.name = name
    ac.attr_key1[name] = key
    ac.attr_key2[key] = name
    ac.attr_key3[name] = da.title
	table.insert(ac.attribute,key)
	if da.is_race then
		table.insert(list2,da)
	else
		table.insert(list1,da)
	end
    register(key)
    --attribute_type[key] = true
end

table.sort(list1,function(a,b)
	return a.index<b.index
end)
table.sort(list2,function(a,b)
	return a.index<b.index
end)

local tx = {
	['基础攻击'] = true,
	['生命上限'] = true,
	['基础力量'] = true,
	['基础敏捷'] = true,
	['基础智力'] = true,
	['基础护甲'] = true,
	['全属性'] = true,
}

function atts_tip(self)
    local tips = {}
	local owner = self.owner
    for _,da in ipairs(list1) do
		local name = da.name
		local num = tonumber(self[name])
		local ex = ''
		if num and num~=0 then
			if num~=0 and tx[name] and owner then
				--local r = owner:get('装备加成')
				--if r>0 then
				--	ex = ('|cff00ff00+%0.0f|r '):format(r*num/100)
				--end
			end
			name = ac.attr_key3[name] or name
			if num<10 and num>-10 and num~=math.ceil(num) then
                local tip = ('|cffff9900%s|r%+0.1f%s'):format(name,num,ex)
				table.insert(tips,tip)
			else
				local tip = ('|cffff9900%s|r%+0.0f%s'):format(name,num,ex)
				table.insert(tips,tip)
			end
        end
	end
    for _,da in ipairs(list2) do
		local name = da.name
		local num = tonumber(self[name])
		if num and num~=0 then
			name = ac.attr_key3[name] or name
			if num<10 and num>-10 and num~=math.ceil(num) then
				local tip = ('|cffff9900%s|r%+0.1f%%'):format(name,num)
				table.insert(tips,tip)
			else
				tip = ('|cffff9900%s|r%+0.0f%%'):format(name,num)
				table.insert(tips,tip)
			end
        end
	end
    
    if #tips>0 then
        return table.concat(tips,'|n') .. '|n'
    end
	return ''
end




local types ={
	{1000000000000,'兆','z'},
	{100000000,'亿','y'},
	{10000,'万','w'},
}

--带单位的数字文本
function format_number1(num)
	local num = tonumber(num)
	for i=1,#types do
		local max = types[i][1]
		local ty = types[i][2]
		if num>=max then
			num = num/max
			if num>100 then
				return string.format('%.0f%s',num,ty)
			else
				return string.format('%.1f%s',num,ty)
			end
		end
	end
	return math.ceil(num)
end

function format_number1_ex(num)
	local num = tonumber(num)
	for i=1,#types do
		local max = types[i][1]
		local ty = types[i][3]
		if num>=max*10 then
			num = num/max
			return string.format('%.0f%s',num,ty)
		end
	end
	return math.ceil(num)
end

function format_number2(num)
	local str = ('%0.f'):format(tonumber(num))
    local len = str:len()
    local num = math.floor((len-1)/3)
    for i=1,num do
        local a = len - 3*i
        str = ('%s,%s'):format(str:sub(1,a),str:sub(a+1,-1))
    end
    return str
end


local function split_int(data,pattern)
    local strs = split_string(tostring(data),pattern)
    for i,s in ipairs(strs) do
        strs[i] = tonumber(s)
    end
    return strs
end

local function table_mul(data,rate)
    local new = {}
    if type(data)=='number' then
        return data * rate
    end

    for i,num in ipairs(data) do
        new[i] = data[i] * rate
    end
    return new
end

ac.game:event '游戏-注册属性'(function(_,class,name,data)
end)

